package com.xr.oa.announce;

import java.util.List;

import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;

/**
 * classViewModel
 */
public class classViewModel extends DefaultViewModel {
  public void deptRoleSetSave(ParameterSet parameters, ParameterSet outParameters)
      throws Exception {
    Dataset deptDs= getDataset("datasetDept");
    Dataset roleDs= getDataset("datasetRole");
    String announceId=deptDs.parameters().getString("info_announce_id");
    DBStatement dbs = new DBStatement();
   
    deptDs.moveFirst();
    while (!deptDs.isLast()) {
     // System.out.println(deptDs.getString("PROVIDER_ID")+"=="+deptDs.getString("PROVIDER_NAME")+"==="+deptDs.getBoolean("status"));
      if(deptDs.getBoolean("select")){
    	  dbs.setSql("select * from oa_info_dept_role where INFO_ANNOUNCE_ID="+announceId
    	  		+" and DEPT_ROLE_ID="+deptDs.getString("PROVIDER_ID")+" and TYPE=1");
    	  List list=dbs.queryForList();
    	  
    	  if(list.size()==0){
    	  dbs.setSql("insert into oa_info_dept_role" +
    	  		"(INFO_DEPT_ROLE_ID,INFO_ANNOUNCE_ID,DEPT_ROLE_ID,TYPE)" +
    	  		"values(general.nextval,"+announceId+","+deptDs.getString("PROVIDER_ID")+",1)");
    	  dbs.execute();
    	  }
      }else{
    	  dbs.setSql("delete from oa_info_dept_role " +
    	  		"where INFO_ANNOUNCE_ID="+announceId
    	  		+" and DEPT_ROLE_ID="+deptDs.getString("PROVIDER_ID")+" and TYPE=1");
      	  dbs.execute();
      }
      
      deptDs.moveNext();
  
      
    }
    roleDs.moveFirst();
    while (!roleDs.isLast()) {
        //System.out.println(deptDs.getString("PROVIDER_ID")+"=="+deptDs.getString("PROVIDER_NAME")+"==="+deptDs.getBoolean("status"));
        if(roleDs.getBoolean("select")){
      	  dbs.setSql("select * from oa_info_dept_role where INFO_ANNOUNCE_ID="+announceId
      	  		+" and DEPT_ROLE_ID="+roleDs.getString("ROLE_INS_ID")+" and TYPE=2");
      	  List list=dbs.queryForList();
      	  
      	  if(list.size()==0){
      	  dbs.setSql("insert into oa_info_dept_role" +
      	  		"(INFO_DEPT_ROLE_ID,INFO_ANNOUNCE_ID,DEPT_ROLE_ID,TYPE)" +
      	  		"values(general.nextval,"+announceId+","+roleDs.getString("ROLE_INS_ID")+",2)");
      	  dbs.execute();
      	  }
        }else{
      	  dbs.setSql("delete from oa_info_dept_role " +
      	  		"where INFO_ANNOUNCE_ID="+announceId
      	  		+" and DEPT_ROLE_ID="+roleDs.getString("ROLE_INS_ID")+" and TYPE=2");
        	  dbs.execute();
        }
        
        roleDs.moveNext();
    
        
      }
    
  dbs.close();
  }

}
